//
//  CZVistorView.swift
//  SinaWeibo
//
//  Created by 浩记 on 16/3/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZVistorView: UIView {
    
    //点击注册闭包（CZBaseTableViewController准备了闭包）
    var clickRegisterClosrue : (() -> ())?
    
    //点击登录闭包（CZBaseTableViewController准备了闭包）
    var clickLoginClosrue : (() -> ())?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override init(frame: CGRect) {
        
        
        //先实现父类方法在写子类自己的方法，属性就要在父类方法之前
        super.init(frame: frame)
        //背景颜色
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        //准备子控件
        prepareUI()
    }
    
    ///设置访客视图
    func setVistor (imageString: String,LaBelString: String) {
        
        //隐藏转轮
        iconView.hidden = true
        
        //把遮盖隐藏，或者推到视图最后一层
        sendSubviewToBack(coverView)
        
        //重新设置小房子视图图片和消息文字
        homeView.image = UIImage(named: imageString)
        messageLabel.text = LaBelString
        
    }

    func startRotation () {
        //创建核心动画
        let rotation = CABasicAnimation(keyPath: "transform.rotation")
        rotation.toValue = M_PI * 2  //旋转角度
        rotation.duration = 20 //旋转到那个角度所用时间
        rotation.repeatCount = MAXFLOAT // 旋转次数
        rotation.removedOnCompletion = false//完成动画的时候不要移除
        
        iconView.layer.addAnimation(rotation, forKey: nil)
    }
    
    private func prepareUI () {
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //给每一个子控件添加约束(frame)
        //首先关闭系统自动为我们设置的约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false

        //iconView转轮  CenterX
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //CenterY
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //coverView遮盖
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        //小房子  CenterX与转轮CenterX重合
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //CenterY与转轮CenterY重合
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //消息Label
        //宽度240  : toItem为nil时 attribute必须填写NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        //CenterX 和转轮CenterX重合
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //top和转轮bottom距离16
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //注册按钮
        //左边和消息的左边重合
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //顶部和消息的底部距离16
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽100
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //高35
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //登录按钮
        //右边和消息的右边重合
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        //顶部和消息的底部距离16
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽100
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //高35
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
    }
    
    //MARK:- 点击按钮事件
    ///点击注册
    func didClickRegisterButton() {
        clickRegisterClosrue?()
    }
    ///点击登录
    func didClickLoginButton() {
        clickLoginClosrue?()
    }
    
    //MARK:- 懒加载子控件
    //转轮--用闭包
//    private lazy var iconView : UIImageView = {
//        var icon = UIImageView()
//        icon.image = UIImage(named: "visitordiscover_feed_image_smallicon")
//        return icon
//    }()
    //不用闭包--不私有，外界要用到
    lazy var iconView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //小房子
//    private lazy var homeView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    lazy var homeView : UIImageView = UIImageView()
    
    //消息Label,,因为要对label设置较多属性，所以要用闭包
    private lazy var messageLabel : UILabel = {
        let label = UILabel()
        
//        label.text = "关注一些人,看看有上面惊喜"
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(18)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        return label
    }()
    
    //注册按钮
    private lazy var registerButton : UIButton = {
        let button = UIButton()
        
        //按钮背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //按钮文字颜色
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        //按钮文字
        button.setTitle("注册", forState: UIControlState.Normal)
        //增加点击事件
        button.addTarget(self, action: "didClickRegisterButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    //登录按钮
    private lazy var loginButton : UIButton = {
        let login = UIButton()
        
        //按钮背景图片
        login.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //按钮文字颜色
        login.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        //按钮文字
        login.setTitle("登录", forState: UIControlState.Normal)
        //增加点击事件
        login.addTarget(self, action: "didClickLoginButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return login
    }()
    
    //遮盖图片
    lazy var coverView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

}
